home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Palm Finder 2 / Src / Panes / battery.cpp next >
Encoding:
Text File  |  2001-06-23  |  1.5 KB  |  91 lines

  1. // battery.cpp
  2.  
  3.  
  4. // includes
  5. #include "battery.h"
  6. #include "drawing.h"
  7. #include "Finder_res.h"
  8.  
  9.  
  10. // constants
  11. const UInt32        k_update_interval = 30; // seconds
  12.  
  13.  
  14. //
  15. // constructor
  16. //
  17. battery::battery(FormGadgetType* gadgetP, view* in_superview): 
  18.     pane(&gadgetP->rect, in_superview)
  19. {
  20.     reset_idle_timer();
  21. }
  22.  
  23. //
  24. // destructor
  25. //
  26. battery::~battery() {
  27.     // do nothing
  28. }
  29.  
  30. #pragma mark -
  31.  
  32. //
  33. // draw_self()
  34. //
  35. void
  36. battery::draw_self() {
  37.     // variables
  38.     UInt16                 battery_level, warn_threshold, critical_threshold, max_ticks;
  39.     SysBatteryKind     kind;
  40.     Boolean                plugged_in;
  41.     unsigned char        percent;
  42.     Int16                battery_icon_number, bitmap_id;                    
  43.     
  44.     // get battery info
  45.     battery_level = SysBatteryInfo (false, &warn_threshold, &critical_threshold, &max_ticks, &kind, &plugged_in, &percent);
  46.     
  47.     // we could create and use battery icons with bolts if charging, or with AC plug overlay
  48.     
  49.     // calculate battery icon number
  50.     battery_icon_number = Int16(percent) / 10;
  51.     if (battery_icon_number>9) 
  52.         battery_icon_number = 9;
  53.     bitmap_id = Battery0Bitmap + battery_icon_number;
  54.     
  55.     // get battery gadget
  56.     int    x = m_bounds.topLeft.x;
  57.     int    y = m_bounds.topLeft.y;
  58.     draw_bitmap(bitmap_id, x, y, left_align, top_align);
  59. }
  60.  
  61.  
  62. // 
  63. // idle_self()
  64. //
  65. void
  66. battery::idle_self() {
  67.     UInt32 now = TimGetSeconds();
  68.     
  69.     if (now>=m_idle_timer) {
  70.         draw();
  71.         reset_idle_timer();
  72.     }
  73. }
  74.  
  75. // 
  76. // reset_idle_timer()
  77. //
  78. void
  79. battery::reset_idle_timer() {
  80.     set_idle_timer (k_update_interval);
  81. }
  82.  
  83. // 
  84. // set_idle_timer()
  85. //
  86. void
  87. battery::set_idle_timer(UInt32 interval) {
  88.     m_idle_timer = TimGetSeconds() + interval;
  89. }
  90.  
  91.